/**
 * Created by Administrator on 2017/3/22 0022.
 */
var container;
var camera, scene, rendererThree;
var beta = 0;
var gamma = 0;
var controls;
var bool_render;
var bgColor=0x0;
var bool;
var lightLine;
var timeDetal=0.001;
var bool_out;
function threeInit() {
    container = document.getElementById('div_3d');

    scene = new THREE.Scene();

    rendererThree = new THREE.WebGLRenderer({
        alpha:true,
        antialias: true
    });
    rendererThree.setSize(window.innerWidth, window.innerHeight);
    rendererThree.setClearColor(0x0, 1.0);
    // rendererThree.setPixelRatio(window.devicePixelRatio);
    // rendererThree.autoClear = false;
    container.appendChild(rendererThree.domElement);

    camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.01, 4000 );
    camera.position.set(0, 0, 300);
    controls = new THREE.OrbitControls(camera, rendererThree.domElement);
    controls.maxDistance = 500;
    controls.dampingFactor=0.1;
    controls.minAzimuthAngle=-60*Math.PI/180;
    controls.maxAzimuthAngle=60*Math.PI/180;
    controls.minPolarAngle = 30*Math.PI/180;; // radians
    controls.maxPolarAngle = 120*Math.PI/180; // radians

    lightLine=LightLineShader.create();
    scene.add(lightLine.mesh);

    onWindowResize();

    window.addEventListener('resize', onWindowResize, false);
    window.addEventListener('deviceorientation', function (e) {
        beta = (e.beta-15) / 2000;
        lightLine.mesh.rotation.y=e.gamma/100;
        // lightLine.mesh.rotation.x= beta;

    });
    bool_render = true;


    animate();
    //
}

function animate() {
    if(bool_render)
    {

        requestAnimationFrame( animate );
        render();
    }
}
function render() {

    if(bool_out)
    {
        timeDetal+=0.0003;
    }
    //
    lightLine.kaleShader.uniforms.resolution.value.x = 1;
    lightLine.kaleShader.uniforms.resolution.value.y = 1;
    lightLine.kaleShader.uniforms.time.value+=timeDetal;

    if(lightLine.mesh.position<0)
    {
        lightLine.mesh.position.z=0;
    }

    controls.update();
    rendererThree.render( scene, camera );
}

function onWindowResize(event) {
    // windowHalfX = window.innerWidth / 2;
    // windowHalfY = window.innerHeight / 2;
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    rendererThree.setSize(window.innerWidth, window.innerHeight);

}
function threeOut()
{


    //lightLine.kaleShader.uniforms.color.value.
    //var vec3={r:0.1,g:0,b:0}
    var _color={color:0x111111}
    TweenMax.to(lightLine.kaleShader.uniforms.alpha,0.5,{value:0,ease:Linear.easeNone,onComplete:function()
    {
        lightLine.kaleShader.uniforms.color.value=new THREE.Color(0x996633);
    }});
    TweenMax.to(lightLine.kaleShader.uniforms.alpha,2,{delay:1,ease:Linear.easeNone,value:1,onUpdate:function()
    {
        if(lightLine.kaleShader.uniforms.alpha.value>0.2)
        {
            bool_out=true;
        }
    }});

    TweenMax.to(lightLine.mesh.position,2.5,{delay:3,z:300,ease:Cubic.easeIn});
    TweenMax.to(lightLine.kaleShader.uniforms.alpha,2.5,{delay:3,ease:Cubic.easeIn,value:1,onComplete:function () {
        var evt=new CustomEvent('Game_PageIn',{detail:'mc'})
        document.dispatchEvent(evt);
    }});


}
// init();
// downHandler();